#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define MAX_SIZE 100010

typedef struct {
    long long data[MAX_SIZE];
    int top;
} Stack;

void initStack(Stack* stack) {
    stack->top = -1;
}

void push(Stack* stack, long long value) {
    stack->data[++stack->top] = value;
}

long long pop(Stack* stack) {
    return stack->data[stack->top--];
}

long long calculatePostfix(char* s) {
    Stack stack;
    initStack(&stack);
    int len = strlen(s);
    int i = 0;

    while (i < len) 
    {
        if (isdigit(s[i])) 
        {
            long long num = 0;
            while (i < len && s[i] != '#') {
                num = num * 10 + (s[i] - '0');
                i++;
            }
            if (s[i] == '#') i++;
            push(&stack, num);
        }
        else 
        {
            long long right = pop(&stack);
            long long left = pop(&stack);
            long long result = 0;

            switch (s[i]) 
            {
            case '+':
                result = left + right;
                break;
            case '-':
                result = left - right;
                break;
            case '*':
                result = left * right;
                break;
            }
            push(&stack, result);
            i++; 
        }
    }
    return pop(&stack);
}

int main() 
{
    int T;
    scanf_s("%d", &T); 
    getchar(); 

    while (T--) 
    {
        char s[MAX_SIZE];
        fgets(s, MAX_SIZE, stdin);

        size_t len = strlen(s);
        if (len > 0 && s[len - 1] == '\n')
        {
            s[len - 1] = '\0';
        }

        long long result = calculatePostfix(s);
        printf("%lld\n", result);
    }

    return 0;
}